{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Wilkinson Power Divider"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this notebook we create a [Wilkinson power divider](https://www.microwaves101.com/encyclopedias/wilkinson-power-splitters), which splits an input signal into two equals phase output signals. Theoritecal results about this circuit are exposed in reference [1]. Here we will reproduce the ideal circuit illustrated below and discussed in reference [2]. In this example, the circuit is designed to operate at 1 GHz.\n",
    "\n",
    "![](wilkinson_power_divider.png)\n",
    "\n",
    " - [1] P. Hallbjörner, Microw. Opt. Technol. Lett. 38, 99 (2003).\n",
    " - [2] Microwaves 101: \"Wilkinson Power Splitters\" "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# standard imports\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import skrf as rf\n",
    "rf.stylely()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# frequency band\n",
    "freq = rf.Frequency(start=0, stop=2, npoints=501, unit='GHz')\n",
    "\n",
    "# characteristic impedance of the ports\n",
    "Z0_ports = 50\n",
    "\n",
    "# resistor\n",
    "R = 100\n",
    "line_resistor = rf.media.DefinedGammaZ0(frequency=freq, Z0=R)\n",
    "resistor = line_resistor.resistor(R, name='resistor')\n",
    "\n",
    "# branches\n",
    "Z0_branches = np.sqrt(2)*Z0_ports\n",
    "beta = freq.w/rf.c\n",
    "line_branches = rf.media.DefinedGammaZ0(frequency=freq, Z0=Z0_branches, gamma=0+beta*1j)\n",
    "\n",
    "d = line_branches.theta_2_d(90, deg=True)  # @ 90°(lambda/4)@ 1 GHz is ~ 75 mm\n",
    "branch1 = line_branches.line(d, unit='m', name='branch1')\n",
    "branch2 = line_branches.line(d, unit='m', name='branch2')\n",
    "\n",
    "# ports\n",
    "port1 = rf.Circuit.Port(freq, name='port1', z0=50)\n",
    "port2 = rf.Circuit.Port(freq, name='port2', z0=50)\n",
    "port3 = rf.Circuit.Port(freq, name='port3', z0=50)\n",
    "\n",
    "# Connection setup\n",
    "#┬Note that the order of appearance of the port in the setup is important \n",
    "connections = [\n",
    "           [(port1, 0), (branch1, 0), (branch2, 0)],\n",
    "           [(port2, 0), (branch1, 1), (resistor, 0)],\n",
    "           [(port3, 0), (branch2, 1), (resistor, 1)]\n",
    "        ]\n",
    "\n",
    "# Building the circuit\n",
    "C = rf.Circuit(connections)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The circuit setup can be checked by visualising the circuit graph (this requires the python package `networkx` to be available). "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "C.plot_graph(network_labels=True, edge_labels=True, port_labels=True, port_fontize=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's look to the scattering parameters of the circuit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fig, (ax1,ax2) = plt.subplots(2, 1, sharex=True)\n",
    "C.network.plot_s_db(ax=ax1, m=0, n=0,  lw=2)  # S11\n",
    "C.network.plot_s_db(ax=ax1, m=1, n=1,  lw=2)  # S22\n",
    "ax1.set_ylim(-90, 0)\n",
    "C.network.plot_s_db(ax=ax2, m=1, n=0,  lw=2)  # S21\n",
    "C.network.plot_s_db(ax=ax2, m=2, n=0,  ls='--', lw=2)  # S31\n",
    "ax2.set_ylim(-4, 0)\n",
    "fig.suptitle('Ideal Wilkinson Divider @ 1 GHz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
